package com.leoman.spinningmachine.http;


import android.util.JsonReader;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.leoman.spinningmachine.bean.BannerBean;
import com.leoman.spinningmachine.bean.CardRecordBean;
import com.leoman.spinningmachine.bean.DayCardInfoBean;
import com.leoman.spinningmachine.bean.DoorBean;
import com.leoman.spinningmachine.bean.InvitationBean;
import com.leoman.spinningmachine.bean.LeaveBean;
import com.leoman.spinningmachine.bean.LeaveGroup;
import com.leoman.spinningmachine.bean.LeaveTypeBean;
import com.leoman.spinningmachine.bean.LeavespBean;
import com.leoman.spinningmachine.bean.MyMessageBean;
import com.leoman.spinningmachine.bean.NewsListBean;
import com.leoman.spinningmachine.bean.NoFunBean;
import com.leoman.spinningmachine.bean.PermissionBean;
import com.leoman.spinningmachine.bean.QyNoticeBean;
import com.leoman.spinningmachine.bean.RmhdBean;
import com.leoman.spinningmachine.bean.ZxdtBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.constant.Urls;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;
import com.spurs.retrofit.APIClient;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/5/24.
 * 接口API
 */

public class WebServiceApi {

    static WebServiceApi instance;

    public WebServiceApi() {
    }

    // 单例模式
    public static WebServiceApi getInstance() {
        if (instance == null) {
            synchronized (WebServiceApi.class) {
                if (instance == null)
                    instance = new WebServiceApi();
            }
        }
        return instance;
    }

    /**
     * 1 发送验证码
     *
     * @param codeType 找回密码register
     */
    public void sendcodeRequest(String phone, String codeType, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("phone", phone);
        param.put("codeType", codeType);
        APIClient.getInstance().getAPIService().PostAPI(Urls.SENDCODE, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 注册
     *
     * @param workId
     * @param phone
     * @param code
     * @param pwd
     * @param confirmPwd
     * @param phoneMac
     * @param listener
     * @param tag
     */
    public void register(String workId, String phone, String code, String pwd, String confirmPwd, String phoneMac, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("workId", workId);
        param.put("phone", phone);
        param.put("code", code);
        param.put("pwd", pwd);
        param.put("confirmPwd", confirmPwd);
        param.put("phoneMac", phoneMac);
        param.put("type", "1");
        APIClient.getInstance().getAPIService().PostAPI(Urls.REGISTER, param).enqueue(new APICallback(listener, tag));
    }


    /**
     * 登录
     *
     * @param phone
     * @param password
     * @param listener
     * @param phoneMac
     * @param registrationID 2.1.9版本新增获取registration id block接口。
     * @param tag
     */
    public void login(String phone, String password, String phoneMac, String registrationID, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("phone", phone);
        param.put("password", password);
        param.put("phoneMac", phoneMac);
        param.put("registrationID", registrationID);
        APIClient.getInstance().getAPIService().PostAPI(Urls.LOGIN, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 修改密码
     *
     * @param phone
     * @param code
     * @param pwd
     * @param confirmPwd
     * @param listener
     * @param tag
     */
    public void upPwd(String phone, String code, String pwd, String confirmPwd, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("phone", phone);
        param.put("code", code);
        param.put("pwd", pwd);
        param.put("confirmPwd", confirmPwd);
        APIClient.getInstance().getAPIService().PostAPI(Urls.UP_PWD, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 修改用户昵称
     *
     * @param token
     * @param nickname
     * @param listener
     * @param tag
     */
    public void upNickName(String token, String nickname, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("nickname", nickname);
        APIClient.getInstance().getAPIService().PostAPI(Urls.UP_NICKNAME, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 修改用户昵称
     *
     * @param token
     * @param headImage
     * @param listener
     * @param tag
     */
    public void upHeadImage(String token, String headImage, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("headImage", headImage);
        APIClient.getInstance().getAPIService().PostAPI(Urls.UP_HEADIMAGE, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 获取某年月天的打卡记录数据
     *
     * @param token
     * @param date     yyyy-MM-dd
     * @param listener
     * @param tag
     */
    public void getMonthRecordData(String token, String date, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("date", date);
        APIClient.getInstance().getAPIService().PostAPI(Urls.CARDRECORD, param).enqueue(new APICallback(listener, new TypeToken<List<DayCardInfoBean>>() {
        }.getType(), tag));
    }

    /**
     * 打卡列表
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void queryRecordList(String token, String page, String size, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("page", page);
        param.put("size", size);
        APIClient.getInstance().getAPIService().PostAPI(Urls.QUERYRECORDLIST, param).enqueue(new APICallback(listener, new TypeToken<List<CardRecordBean>>() {
        }.getType(), tag));

    }

    /**
     * 查看新闻动态
     *
     * @param type
     * @param listener
     * @param tag
     */
    public void newsQueryList(String type, String page, String size, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("type", type);
        param.put("page", page);
        param.put("size", size);
        APIClient.getInstance().getAPIService().PostAPI(Urls.newsQueryList, param).enqueue(new APICallback(listener, new TypeToken<List<NewsListBean>>() {
        }.getType(), tag));
    }

    /**
     * 新闻详情
     *
     * @param token
     * @param newsId
     * @param listener
     * @param tag
     */
    public void newsDetail(String token, String newsId, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("newsId", newsId);
        APIClient.getInstance().getAPIService().PostAPI(Urls.newsQueryDetail, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 企业信息
     *
     * @param listener
     * @param tag
     */
    public void newsQueryEnterpriseInfo(APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("type", "1");
        APIClient.getInstance().getAPIService().PostAPI(Urls.newsqueryEnterpriseInfo, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 查询我的请假列表
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void leaveQueryList(String token, String page, String size, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("page", page);
        param.put("size", size);
        APIClient.getInstance().getAPIService().PostAPI(Urls.LEAVE_QUERYMY, param).enqueue(new APICallback(listener, new TypeToken<List<LeaveGroup>>() {
        }.getType(), tag));
    }

    /**
     * 审批列表
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void LeavespList(String token, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        APIClient.getInstance().getAPIService().PostAPI(Urls.SP_QUERYLIST, param).enqueue(new APICallback(listener, new TypeToken<List<LeavespBean>>() {
        }.getType(), tag));
    }

    /**
     * 审批人查看某用户的请假列表
     *
     * @param token
     * @param userId
     * @param page
     * @param size
     * @param listener
     * @param tag
     */
    public void queryUserLeaveList(String token, String userId, String page, String size, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("userId", userId);
        param.put("page", page);
        param.put("size", size);
        APIClient.getInstance().getAPIService().PostAPI(Urls.QUERYUSERLEAVELIST, param).enqueue(new APICallback(listener, new TypeToken<List<LeavespBean>>() {
        }.getType(), tag));
    }

    /**
     * 请假类型
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void leaveType(String token, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        APIClient.getInstance().getAPIService().PostAPI(Urls.LEAVE_TYPE, param).enqueue(new APICallback(listener, new TypeToken<List<LeaveTypeBean>>() {
        }.getType(), tag));
    }

    /**
     * 审批
     *
     * @param token
     * @param leaveId
     * @param status
     * @param listener
     * @param tag
     */
    public void updateLeave(String token, String leaveId, String status, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("leaveId", leaveId);
        param.put("status", status);
        APIClient.getInstance().getAPIService().PostAPI(Urls.SP_UPDATELEAVE, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 上传图片
     *
     * @param token
     * @param leaveId
     * @param status
     * @param listener
     * @param tag
     */
    public void uploadUserImage(String token, String leaveId, String status, APICallback.OnResponseListener listener, int tag) {

        Map<String, RequestBody> map = new HashMap<>();
//        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"),file);
//        for (int i = 0; i < files.size(); i++) {
//            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), files.get(i));
//            map.put("file" + i + "\";filename=\"" + files.get(i).getName(), requestBody);
//        }


//        APIClient.getInstance().getAPIService().PostImageAPI();
    }

    /**
     * 新增请假
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void leaveNew(String token, String startTime, String endTime, String leaveDays, String typeId, String workNum,
                         String reason, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("leaveDays", leaveDays);
        param.put("typeId", typeId);
        param.put("workNum", workNum);
        param.put("reason", reason);
        APIClient.getInstance().getAPIService().PostAPI(Urls.LEAVE_NEW, param).enqueue(new APICallback(listener, new TypeToken<List<LeaveBean>>() {
        }.getType(), tag));
    }

    /**
     * 首页中最新动态
     *
     * @param str
     * @param listener
     * @param tag
     */
    public void queryZxDt(String str, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", "");
        APIClient.getInstance().getAPIService().PostAPI(Urls.ZXDT, param).enqueue(new APICallback(listener, new TypeToken<List<ZxdtBean>>() {
        }.getType(), tag));
    }

    /**
     * 首页中热门活动
     *
     * @param str
     * @param listener
     * @param tag
     */
    public void queryRmhd(String str, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", "");
        APIClient.getInstance().getAPIService().PostAPI(Urls.RMHD, param).enqueue(new APICallback(listener, new TypeToken<List<RmhdBean>>() {
        }.getType(), tag));
    }

    /**
     * 企业通知
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void qynoticeList(String token, String page, String size, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("page", page);
        param.put("size", size);
        APIClient.getInstance().getAPIService().PostAPI(Urls.QY_NOTICELIST, param).enqueue(new APICallback(listener, new TypeToken<List<QyNoticeBean>>() {
        }.getType(), tag));
    }


    /**
     * 消息
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void messageList(String token, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        APIClient.getInstance().getAPIService().PostAPI(Urls.MESSAGEMYLIST, param).enqueue(new APICallback(listener, new TypeToken<List<MyMessageBean>>() {
        }.getType(), tag));
    }

    /**
     * 首页banner
     *
     * @param listener
     * @param tag
     */
    public void banner(APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", "");
        APIClient.getInstance().getAPIService().PostAPI(Urls.BANNER, param).enqueue(new APICallback(listener, new TypeToken<List<BannerBean>>() {
        }.getType(), tag));
    }

    /**
     * 修改手机号
     *
     * @param token
     * @param phone
     * @param code
     * @param listener
     * @param tag
     */
    public void upPhoneNum(String token, String phone, String code, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("phone", phone);
        param.put("code", code);
        APIClient.getInstance().getAPIService().PostAPI(Urls.UP_PHONENUM, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 用户的权限
     *
     * @param token
     * @param listener
     * @param tag
     */
    public void queryMyPermission(String token, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        APIClient.getInstance().getAPIService().PostAPI(Urls.RIGHT_MYLIST, param).enqueue(new APICallback(listener, new TypeToken<List<PermissionBean>>() {
        }.getType(), tag));
    }

    /**
     * 二维码开门
     *
     * @param data
     * @param listener
     * @param tag
     */
    public void qrCodeOpenDoor(String token, String data, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("data", data);
        APIClient.getInstance().getAPIService().PostAPI(Urls.QR_CODE_OPEN, param).enqueue(new APICallback(listener, new TypeToken<List<PermissionBean>>() {
        }.getType(), tag));
    }

    /**
     * 根据蓝牙mac获取门禁名字
     *
     * @param mac
     * @param listener
     * @param tag
     */
    public void getDoorName(String mac, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("mac", mac);
        APIClient.getInstance().getAPIService().PostAPI(Urls.BLUETOOTH_DOORNAME, param).enqueue(new APICallback(listener, new TypeToken<List<DoorBean>>() {
        }.getType(), tag));
    }

    /**
     * 添加开门记录
     *
     * @param token
     * @param bluetoothMac
     * @param status
     * @param listener
     * @param tag
     */
    public void addOpenDoorHistory(String token, String bluetoothMac, String status, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", token);
        param.put("bluetoothMac", bluetoothMac);
        param.put("status", status);
        APIClient.getInstance().getAPIService().PostAPI(Urls.BLUETOOTH_OPENHISTORY, param).enqueue(new APICallback(listener, tag));
    }


    /**
     * 摄像头
     *
     * @param listener
     * @param tag
     */
    public void camera(String pageNo, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("pageNo", pageNo);
        param.put("pageSize", String.valueOf(Constant.pageSize));
        param.put("token", SharedPreferencesUtils.getInstance().getString(Constant.TOKEN));
        APIClient.getInstance().getAPIService().PostAPI(Urls.CAMERA, param).enqueue(new APICallback(listener, new TypeToken<List<NoFunBean>>() {
        }.getType(), tag));
    }


    /**
     * 授权开门
     *
     * @param listener
     * @param tag
     */
    public void opendoor(APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", SharedPreferencesUtils.getInstance().getString(Constant.TOKEN));
        APIClient.getInstance().getAPIService().PostAPI(Urls.OPEN_DOOR, param).enqueue(new APICallback(listener, new TypeToken<List<InvitationBean>>() {
        }.getType(), tag));
    }


    /**
     * 异地打卡
     *
     * @param listener
     * @param tag
     */
    public void yidi(String lon, String lat, APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("uid", SharedPreferencesUtils.getInstance().getString(Constant.USERID));
        param.put("lon", lon);
        param.put("lat", lat);
        APIClient.getInstance().getAPIService().PostAPI(Urls.YIDI, param).enqueue(new APICallback(listener, tag));
    }

    /**
     * 分享链接
     * @param url
     * @param listener
     * @param tag
     */
    public void dUrl(String url,APICallback.OnResponseListener listener, int tag) {
        HashMap<String, String> param = new HashMap<>();
        param.put("url",url);
        APIClient.getInstance().getAPIService().PostAPI(Urls.DURL, param).enqueue(new APICallback(listener, tag));
    }

}